package com.qsd.StudentSystem.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.qsd.common.util.PageUtil;
import com.qsd.common.util.ResultMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qsd.StudentSystem.dao.ClazzDao;
import com.qsd.StudentSystem.dao.GradeDao;
import com.qsd.StudentSystem.service.CourseService;
import com.qsd.common.po.Course;
import com.qsd.common.po.Grade;
import com.qsd.common.po.Student;
import com.qsd.common.util.JdbcUtil;
import com.qsd.common.vo.PageVo;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Controller
public class IndexController {

    @Resource
    private ClazzDao clazzDao;
    @Resource
    private CourseService courseService;
    @Resource
    private GradeDao gradeDao;

    @RequestMapping("index")
    public String index() {
        return "index";
    }

    @RequestMapping("self")
    public String self(HttpServletRequest request, Model model) {
        log.info("跳转到当前用户详情信息页");
        Student student = (Student) request.getSession().getAttribute("student");
        model.addAttribute("student", student);
        return "self";
    }

    @RequestMapping("search")
    public String search() {
        log.info("跳转到详情页");
        return "search";
    }

    @RequestMapping("course")
    public String course(int page, int limit, HttpServletRequest request, Model model) {
        log.info("获取加入的课程信息");
        Student student = (Student) request.getSession().getAttribute("student");
        List<Course> courses = clazzDao.queryAllCourses(student.getUsername());
        PageVo<Course> coursePage = JdbcUtil.PageUtil(page, limit, courses);
        model.addAttribute("page", page);
        model.addAttribute("count", coursePage.getCount());
        model.addAttribute("courses", coursePage.getData());
        return "course";
    }

    @RequestMapping("grade")
    public String grade(int page, int limit, HttpServletRequest request, Model model) {
        log.info("分数加载");
        Student student = (Student) request.getSession().getAttribute("student");
        List<Grade> grades = gradeDao.queryByUsername(student.getUsername());
        PageVo<Grade> gradePage = JdbcUtil.PageUtil(page, limit, grades);
        model.addAttribute("page", page);
        model.addAttribute("count", gradePage.getCount());
        model.addAttribute("grades", gradePage.getData());
        return "grade";
    }

    @RequestMapping("grade2")
    public String grade2() {
        log.info("跳转到成绩信息页");
        return "grade2";
    }

    /**
     * 查询考试分数列表,条件搜索
     *
     * @param grade
     * @param page
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public ResultMsg fileList(Grade grade, PageUtil page,HttpServletRequest request) {
        log.info("考试分数表格重载");
        Student student = (Student) request.getSession().getAttribute("student");
        List<Grade> iPage = courseService.fileList(grade, page,student);
        return new ResultMsg(0, "", iPage, page.getCount());
    }


    @RequestMapping("about")
    public String about() {
        return "about";
    }

    @RequestMapping("test")
    public String test(Integer id, Model model) {
        log.info("跳转到考试页面:id:{}",id);
        courseService.queryPaperByCourseId(id, model);
        log.info("跳转到考试页:test.html");
        return "test";
    }

}
